package core;
//
//import static org.junit.Assert.*;
//
//import java.io.File;
//import java.io.IOException;
//import java.net.MalformedURLException;
//import java.net.URL;
//import java.security.PrivateKey;
//import java.security.PublicKey;
//import java.util.List;
//
//import org.junit.BeforeClass;
//import org.junit.Test;
//import com.sun.syndication.io.FeedException;
//
//import dustdispatcher.DustDispatcher;
//
//import events.UpdateCompleteEvent;
//import events.UpdateCompleteListener;
//
public class BlogTest {
//	private static final String tests_path = "tests_blog/";
//	private static final String privkey_fname = "keys/publicador-private.key";
//	private static final String pubkey_fname = "keys/publicador-public.key";
//	private static final String URL_FEED = "http://www.informatica64.com/dust/elladodelmal.xml";
//	private static final String FILE_FEED1 = tests_path + "feed1.xml";
//	private static final String FILE_FEED2 = tests_path + "feed2.xml";
//	private static PrivateKey priv_key;
//	private static PublicKey pub_key;
//	private static Blog test_blog;
//	
//	//static Blog blog;
//	@BeforeClass  	
//	public static void setUpClass() throws Exception {
//		new DustDispatcher();
//		URL url = new URL(URL_FEED);
//		
////		priv_key = KeyUtils.recoverPrivateKey(privkey_fname);
////		pub_key = KeyUtils.recoverPublicKey(pubkey_fname);
//		
//		test_blog = new Blog(url, "Test blog");
//		test_blog.update();
//		test_blog.waitForUpdate();
//	}
//	
//	@Test
//	public void testBlog() {
//		Blog blog1, blog2;
//		Post post1, post2;
//		
//		// Correct case, it creates a blog from a file.
//		// Check a few things to validate its work.
//		try {
//			blog1 = new Blog(FILE_FEED1, "test blog 1");
//			blog2 = new Blog(FILE_FEED2, "test blog 2");
//			
//			assertEquals("Un informático en el lado del mal", blog1.getTitle());
//			assertEquals("Security By Default", blog2.getTitle());
//			
//			post1 = blog1.getPost(0);
//			post2 = blog2.getPost(0);
//			
//			assertEquals("Dust: First Blood", post1.getTitle());
//			assertEquals("Me inComodo", post2.getTitle());
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}
//		
//		// Incorrect case, file doesn't exists.
//		try {
//			blog1 = new Blog("this_file_doesnt_exists", "Channel");
//			fail("testBlog doesn't threw an exception.");
//		}
//		catch (Exception e) {}
//		
//		// Incorrect case, file exists but it isn't a XML feed file.
//		try {
//			blog1 = new Blog(tests_path + "file_that_isnt_a_blog.xml", "channel");
//			fail("testBlog doesn't threw an exception.");
//		}
//		catch (Exception e) {}
//	}
//	
////	@Test
////	public void testUpdateBlogFromDate() throws IllegalArgumentException,
////	FeedException, IOException {
////		Blog blog = new Blog(new URL(URL_FEED), "Stupid channel's name");
////		Calendar cal = Calendar.getInstance();
////		Date date_day, date_day_before;
////		int n_posts;
////		
////		cal.set(2011, Calendar.JANUARY, 21);
////		date_day = cal.getTime();
////		cal.set(2011, Calendar.JANUARY, 20);
////		date_day_before = cal.getTime();
////		
////		// Correct case.
////		try {	
////			n_posts = blog.updateFromDate(date_day);
////			assertEquals(5, n_posts);
////			
////			n_posts = blog.updateFromDate(date_day_before);
////			assertEquals(1, n_posts);
////		}
////		catch (Exception e) {
////			e.printStackTrace();
////		}
////		
////		// Incorrect case, the domain doesn't exists.
////		try {
////			blog = new Blog(new URL("http://unexistant.domain.com/feed"), "");
////			blog.updateFromDate(date_day);
////		}
////		catch (Exception e) {}
////		
////		// Incorrect case, the resource is not a feed.
////		try {
////			blog = new Blog(new URL("http://www.google.es"), "");
////			blog.updateFromDate(date_day);
////		}
////		catch (Exception e) {}
////	}
//	
//	/**
//	 * Auxiliar class to check the number of posts in the next tests.
//	 */
//	private class CheckPosts implements UpdateCompleteListener {
//		private int amount;
//		
//		public CheckPosts(int n) {
//			amount = n;
//		}
//		
//		@Override
//		public void handleEvent(UpdateCompleteEvent event) {
//			int n_posts;
//			
//			n_posts = event.getSource().getPosts().size();
//			assertEquals(n_posts, amount);
//		}
//	}
//	
//	@Test
//	public void testUpdateBlog() throws IllegalArgumentException,
//	FeedException, IOException {
//		Blog blog = new Blog(new URL(URL_FEED), "Stupid channel's name");
//		
//		// Correct case, load the 25 posts contained by this blog's feed.
//		try {
//			blog.update(new CheckPosts(25));
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}
//		
//		// Correct case, posts were loaded above so there aren't new posts.
//		try {
//			blog.update(new CheckPosts(0));
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
//	
////	@Test
////	public void testUpdateBlogLastPosts() throws IllegalArgumentException,
////	FeedException, IOException {
////		Blog blog = new Blog(new URL(URL_FEED), "Stupid channel's name");
////		int n_posts;
////		
////		try {
////			n_posts = blog.updateLastPosts(5);
////			assertEquals(n_posts, 5);
////		}
////		catch (Exception e) {
////			e.printStackTrace();
////		}
////		
////		// Correct case, posts were loaded above so there aren't new posts.
////		try {
////			n_posts = blog.updateLastPosts(6);
////			assertEquals(n_posts, 1);
////		}
////		catch (Exception e) {
////			e.printStackTrace();
////		}
////	}
//	
//	// This test checks the methods save() and createBlog() since each one
//	// of them has no meaning without the other.
//	@Test
//	public void testSaveCreateBlog() {
//		File fd;
//		String fname = null;
//		Blog aux_blog;
//		Post test_post, aux_post;
//		
//		// Correct case, it creates a file with the name specified.
//		try {
//			fname = test_blog.save(tests_path);
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}
//		
//		// This test depends on the method createBlog().
//		// Recover the blog from the saved file and check that they are the
//		// same. Since we have no equal() method in the class Blog we will
//		// check a few things that must be the same.
//		try {
//			aux_blog = Blog.createBlog(tests_path + fname);
//			
//			// Check if both blogs have the same title.
//			if (!aux_blog.getTitle().equals(test_blog.getTitle()))
//				fail("Blogs saved and recovered haven't the same title.");
//
//			// Check a few amount of posts have the same titles.
//			test_post = test_blog.getPost(0);
//			aux_post = aux_blog.getPost(0);
//			if (!aux_post.getTitle().equals(test_post.getTitle()))
//				fail("Posts in position 0 haven't the same title.");
//				
//			// Check the content of those posts.
//			if (!aux_post.getContent().equals(test_post.getContent()))
//				fail("Posts in position 0 haven't the same content.");
//			
//			// Delete the file.
//			if (fname != null) {
//				fd = new File(tests_path + fname);
//				if (fd.exists()) {
//					if (!fd.delete())
//						System.out.println("[WARN]: Test file for save" +
//										   " method couldn't be deleted.");
//				}
//				else
//					fail("Couldn't save the file.");
//			}
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}
//		
//		// Incorrect case, trying to load something that is not a blog.
//		try {
//			aux_blog = Blog.createBlog(tests_path + "file_that_isnt_a_blog.xml");
//			fail("testSaveCreateBlog should returned an exception.");
//		}
//		catch (Exception e) {}
//		
//		// Incorrect case, trying to load from an non-existent file.
//		try {
//			aux_blog = Blog.createBlog(tests_path +
//									   "this_file_doesnt_exists.xml");
//			fail("testSaveCreateBlog created a blog from non-existent file?!");
//		}
//		catch (Exception e) {}
//	}
//	
//	@Test
//	public void testGetPost() {
//		Post post;
//		
//		// Correct case.
//		try {
//			post = test_blog.getPost(6);
//			assertEquals("Malware educativo: Botnets & R.A.T.s", post.getTitle());						
//		}
//		catch(Exception e) {
//			e.printStackTrace();
//		}
//		
//		// Incorrect case, index out of bounds positive.
//		try {
//			post = test_blog.getPost(0x01ec0ded);
//			fail("getPost should throw an exception.");
//		}
//		catch(Exception e) {}
//		
//		// Incorrect case, index out of bounds negative.
//		try {
//			post = test_blog.getPost(0xdeadbeef);
//			fail("getPost should throw an exception.");
//		}
//		catch(Exception e) {}
//	}
//	
//	@Test
//	public void testGetPosts() {
//		List<Post> posts;
//		
//		// Correct case.
//		try {
//			posts = test_blog.getPosts();
//			assertEquals(25, posts.size());
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
//	
//	@Test
//	public void testGetPostsByAuthor() {
//		List<Post> posts;
//		
//		// Correct case, there are 24 posts from Maligno and 1 from Alex and 0
//		// from an author that doesn't exists (or at least s/he doesn't write
//		// for this blog).
//		try {
//			posts = test_blog.getPostsByAuthor("noreply@blogger.com (Maligno)");
//			assertEquals(posts.size(), 24);
//			
//			posts = test_blog.getPostsByAuthor("amartin@informatica64.com (Alex)");
//			assertEquals(posts.size(), 1);
//			
//			posts = test_blog.getPostsByAuthor("this is not an author");
//			assertEquals(posts.size(), 0);
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
//	
//	@Test
//	public void testGetAuthors() {
//		// Correct case, there are 2 authors.
//		try {
//			assertEquals(2, test_blog.getAuthors().size());
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
//	
//	@Test
//	public void testGetOwner() {
//		// Correct case.
//		try {
//			assertEquals("noreply@blogger.com (Maligno)",
//						 test_blog.getOwner());
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
//	
//	@Test
//	public void testGetUrl() {
//		// Correct case.
//		try {
//			assertEquals(URL_FEED, test_blog.getUrl().toString());
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}				
//	}
//	
//	@Test
//	public void testGetTitle() {
//		// Correct case.
//		try {
//			assertEquals("Un informático en el lado del mal",
//						 test_blog.getTitle());
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}				
//	}
//	
//	@Test
//	public void testGetContent() {
//		// Correct case.
//		try {
//			assert(test_blog.getContent().endsWith("</rss>\n"));
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}				
//	}
//	
//	@Test
//	public void testGetChannel() {
//		// Correct case.
//		try {
//			assertEquals("Test blog", test_blog.getChannel());
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}				
//	}
//	
//	@Test
//	public void testPublishPostZipped() {
//		File fd;
//		String fzipdustname;
//		// Correct case.
//		try {
////			fzipdustname = test_blog.publishPostZipped(0, tests_path,
////													   priv_key);
//			
//			// Check that the zip-dust file exists and delete it.
////			fd = new File(fzipdustname);
////			if (!fd.exists())
////				fail("testPublishPostZipped doesn't create the file.");
////			else
////				if (!fd.delete())
////					System.out.println("[WARN]: cannot delete file" +
////									   fzipdustname);
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
//	
//	@Test
//	public void testPublishAllPostZipped() {
//		File fd;
//		List<String> list_fnames;
//		// Correct case.
//		try {
////			list_fnames = test_blog.publishAllPostsZipped(tests_path,
////														  priv_key);
//			
//			// Check that each zip-dust file exists and delete it.
////			for (String fzipdustname: list_fnames) {
////				fd = new File(fzipdustname);
////				if (!fd.exists())
////					fail("testPublishPostZipped doesn't create the file.");
////				else
////					if (!fd.delete())
////						System.out.println("[WARN]: cannot delete file" +
////										   fzipdustname);
////			}
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
//	
//	@Test
//	public void testPublishBlog() {
//		String dname = tests_path + "publishblogtest/";
//		File fd;
//		
//		// Create a folder for this test.
//		fd = new File(dname);
//		if (fd.mkdir()) {
////			try {
////				test_blog.publishBlog(dname, priv_key, pub_key);
////			}
////			catch (Exception e) {
////				e.printStackTrace();
////			}
//			
//			Util.rmdirInDepth(dname);
//		}
//		else
//			System.out.println("[WARN]: Cannot create testPublishBlog" +
//							   " directory.");
//	}
//	
//	@Test
//	public void testSearchAndDelete() throws IOException {
//		test_blog.save(tests_path);
//		
//		// Correct case, it must found the the blog saved and delete it.
//		if (!test_blog.searchAndDelete(tests_path))
//			fail("testSearchAndDelete couldn't delete the file " +
//				 "just created!");
//		
//		// Incorrect case, tries to delete a file that doesn't exists.
//		if (test_blog.searchAndDelete(tests_path))
//			fail("testSearchAndDelete has delete something!");
//	}
//	
//	@Test
//	public void testAddUrlAndGetUrls() throws MalformedURLException {
//		List<URL> urls_list;
//		URL aux_url = new URL("http://www.jfajdflkasjdf.com/");
//		
//		if (test_blog.getUrls().size() != 1)
//			fail("Not the quantity of URLs spected.");
//		
//		test_blog.addUrl(aux_url);
//		if (test_blog.getUrls().size() != 2)
//			fail("Added a new URL and not the quantity of URLs spected.");
//		
//		urls_list = test_blog.getUrls();
//		assertEquals(urls_list.get(0).toString(), URL_FEED);
//		assertEquals(urls_list.get(1).toString(), aux_url.toString());
//	}
}